package ltd.smallwang.infrastructure.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import ltd.smallwang.domain.task.Task;
import ltd.smallwang.domain.task.TaskExecuteRecord;
import ltd.smallwang.domain.task.gateway.TaskGateway;
import ltd.smallwang.infrastructure.task.db.TaskExecuteRecordMapper;
import ltd.smallwang.infrastructure.task.db.TaskMapper;
import ltd.smallwang.infrastructure.task.db.convert.TaskConvert;
import ltd.smallwang.infrastructure.task.db.convert.TaskExecuteRecordConvert;
import ltd.smallwang.infrastructure.task.db.model.TaskPo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author justd
 */
@Service
@RequiredArgsConstructor
public class TaskGatewayImpl implements TaskGateway {
  private final TaskMapper taskMapper;

  private final TaskExecuteRecordMapper taskExecuteRecordMapper;

  @Override
  public void save(Task task) {
    TaskPo taskPo = TaskConvert.INSTANCE.convert(task);
    taskMapper.insert(taskPo);
  }

  @Override
  public void modify(Task task) {

  }

  @Override
  public void delete(String id) {

  }

  @Override
  public List<Task> list(Long userId, int pageIndex, int pageSize) {
    QueryWrapper<TaskPo> queryWrapper = new QueryWrapper<>();
    queryWrapper.lambda().eq(TaskPo::getUserId, userId);
    Page<TaskPo> page = new Page<>(pageIndex, pageSize);
    List<TaskPo> taskPos = taskMapper.selectList(page, queryWrapper);
    return taskPos.stream().map(TaskConvert.INSTANCE::convert).toList();
  }

  @Override
  public void saveExecuteRecord(TaskExecuteRecord taskExecuteRecord) {
    taskExecuteRecordMapper.insert(TaskExecuteRecordConvert.INSTANCE.convert(taskExecuteRecord));
  }
}
